/**
 * 
 */
package org.xlab.baseframework.test.service;

import static org.junit.Assert.*;

import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.xlab.baseframework.domain.ISystemUserDomain;
import org.xlab.baseframework.domain.SystemCode;
import org.xlab.baseframework.domain.enumerate.EnumDeletedIndentifying;
import org.xlab.baseframework.exception.RootUserNotFoundException;
import org.xlab.baseframework.exception.SystemCodeDuplicateException;
import org.xlab.baseframework.exception.SystemCodeNotFoundException;
import org.xlab.baseframework.service.api.ISystemCodeService;
import org.xlab.baseframework.service.spi.IUserService;
import org.xlab.baseframework.test.base.BaseTester;

/**
 * @author MaKuBEX
 * 
 */
@RunWith(BaseTester.class)
@ContextConfiguration("classpath:org/xlab/baseframework/conf/module/*/baseframework-*.xml")
public class SystemCodeServiceTestCase {

	private static final Logger log = Logger
			.getLogger(SystemCodeServiceTestCase.class);

	@Autowired
	@Qualifier("bpf_systemCodeService")
	private ISystemCodeService systemCodeService;

	@Autowired
	@Qualifier("bpf_defaultUserService")
	private IUserService userService;

	@Test
	public void testCreateCode() throws SystemCodeNotFoundException,
			SystemCodeDuplicateException {
		log.debug("Creating root user domain...");
		ISystemUserDomain root = null;
		try {
			root = this.userService.getSystemRootUser();
		} catch (RootUserNotFoundException e) {
			e.printStackTrace();
		}
		if (root != null) {
			this.systemCodeService.createSystemCode("TESTING-CODE",
					"TESTING-DESC", 1L, null, root);
			SystemCode testing_code = this.systemCodeService.findByCode(
					"TESTING-CODE", false);
			this.systemCodeService.createSystemCode("TESTING-CODE-1",
					"TESTING-DESC-1", 1L, testing_code, root);
			this.systemCodeService.createSystemCode("TESTING-CODE-2",
					"TESTING-DESC-2", 2L, testing_code, root);
			this.systemCodeService.createSystemCode("TESTING-CODE-3",
					"TESTING-DESC-3", 3L, testing_code, root);
			this.systemCodeService.createSystemCode("TESTING-CODE-4",
					"TESTING-DESC-4", 4L, testing_code, root);
			this.systemCodeService.createSystemCode("TESTING-CODE-5",
					"TESTING-DESC-5", 5L, testing_code, root);
			SystemCode testing_code_1 = this.systemCodeService.findByCode(
					"TESTING-CODE-1", true);
			this.systemCodeService.createSystemCode("TESTING-CODE-1-1",
					"TESTING-DESC-1-1", 1L, testing_code_1, root);
			this.systemCodeService.createSystemCode("TESTING-CODE-1-2",
					"TESTING-DESC-1-2", 2L, testing_code_1, root);
			assertEquals(11, this.systemCodeService.findAll().size());
			testing_code = this.systemCodeService.findByCode("TESTING-CODE",
					false);
			assertEquals(
					5,
					this.systemCodeService.findAllByCategory(testing_code,
							false, true).size());
			assertEquals(
					5,
					this.systemCodeService.findAllByCategory(testing_code,
							false, true).size());
			assertEquals(
					2,
					this.systemCodeService.findAllByCategory(testing_code_1,
							true, true).size());
			SystemCode testing_code_1_1 = this.systemCodeService
					.findAllByCategory(testing_code_1, true, true).get(0);
			this.systemCodeService.deleteSystemCode(testing_code_1_1, root);
			assertEquals(
					1,
					this.systemCodeService.findAllByCategory(testing_code_1,
							true, true).size());
			assertEquals(11, this.systemCodeService.findAll().size());
			assertEquals(EnumDeletedIndentifying.Y,
					testing_code_1_1.getDeletedInd());
		}
	}
}
